package com.future.config.exception;

import com.future.config.result.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

/**
 * 统一异常处理自动配置
 *
 * @author shiyong
 * 2022/3/10 11:22
 */
@EnableWebMvc
@Configuration(proxyBeanMethods = false)
@ConditionalOnBean(CommonExceptionHandleMarkerConfiguration.Marker.class)
@Slf4j
@RestControllerAdvice("com.future")
public class CommonExceptionHandleAutoConfiguration {
    static {
        System.out.println("CommonExceptionHandleAutoConfiguration init....");
    }

    /**
     * 处理业务异常
     *
     * @param e 异常
     * @return org.springframework.http.ResponseEntity<com.riskraider.common.entity.CommonResult>
     * @author shiyong
     * 2019-10-08 17:19
     */
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<CommonResult<Void>> handleBusinessException(BusinessException e) {

        CommonResult<Void> commonResult = new CommonResult<>(CommonResult.FAIL, e.getMessage());

        return new ResponseEntity<>(commonResult, HttpStatus.OK);
    }

    /**
     * 处理参数异常
     *
     * @param e 异常信息
     * @return org.springframework.http.ResponseEntity<com.riskraider.common.entity.CommonResult>
     * @author shiyong
     * 2019/12/27 11:05
     */
    @ExceptionHandler(ParameterException.class)
    public ResponseEntity<CommonResult<Void>> handleParameterException(ParameterException e) {
        CommonResult<Void> commonResult = new CommonResult<>(CommonResult.FAIL, e.getMessage());

        return new ResponseEntity<>(commonResult, HttpStatus.OK);
    }

    /**
     * 处理客户端请求参数异常
     *
     * @param exception 参数异常
     * @return org.springframework.http.ResponseEntity<com.riskraider.common.entity.CommonResult>
     * @author shiyong
     * 2019/10/24 9:14
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<CommonResult<Void>> handleMethodArgumentNotValidException(MethodArgumentNotValidException exception) {

        // 返回一条错误信息
        String message = exception.getBindingResult().getFieldErrors().get(0).getDefaultMessage();

        CommonResult<Void> commonResult = new CommonResult<>(CommonResult.FAIL, message);

        return new ResponseEntity<>(commonResult, HttpStatus.OK);
    }

    /**
     * 处理一般异常
     *
     * @return org.springframework.http.ResponseEntity<com.riskraider.common.entity.CommonResult>
     * @author shiyong
     * 2019-10-08 17:21
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<CommonResult<Void>> handleException(Exception e) {

        CommonResult<Void> commonResult = new CommonResult<>(CommonResult.FAIL, "服务器异常！");

        log.error("服务器异常！", e);

        return new ResponseEntity<>(commonResult, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
